package com.sixth.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.sixth.annotation.PageInfoAdvice;
import com.sixth.bean.MarketFeedback;
import com.sixth.bean.MarketFeedbackExample;
import com.sixth.bean.bo.WXFeedBackBO;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.mapper.MarketFeedbackMapper;
import com.sixth.service.AdminFeedbackService;
import com.sixth.util.MarketInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminFeedbackServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/7 22:06
 **/
@Service
public class AdminFeedbackServiceImpl implements AdminFeedbackService {

    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;


    @PageInfoAdvice
    @Override
    public Object list(BasePageInfo basePageInfo, String username, Integer id) {
        MarketFeedbackExample example = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username.trim() + "%");
        }

        if(id != null){
            criteria.andIdEqualTo(id);
        }

        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        Object marketFeedbacks = marketFeedbackMapper.selectByExample(example);

        return marketFeedbacks;
    }

    @Override
    public int wxSubmit(WXFeedBackBO wxFeedBackBO) {
        //新增一条反馈信息
        MarketFeedback marketFeedback = new MarketFeedback();
        marketFeedback.setUserId(MarketInfoUtil.getMarketUser().getId());
        marketFeedback.setUsername(MarketInfoUtil.getMarketUser().getUsername());
        if(wxFeedBackBO.getMobile() == null){
            marketFeedback.setMobile(MarketInfoUtil.getMarketUser().getMobile());
        }else {
            marketFeedback.setMobile(wxFeedBackBO.getMobile());
        }
        marketFeedback.setFeedType(wxFeedBackBO.getFeedType());
        marketFeedback.setContent(wxFeedBackBO.getContent());
        marketFeedback.setHasPicture(wxFeedBackBO.getHasPicture());
        if(marketFeedback.getHasPicture().equals(true)){
            marketFeedback.setPicUrls(wxFeedBackBO.getPicUrls().toArray(new String[0]));
        }
        marketFeedback.setAddTime(new Date());
        marketFeedback.setUpdateTime(new Date());

        int code = marketFeedbackMapper.insertSelective(marketFeedback);
        if(code == 0){
            return 404;
        }


        return 200;
    }
}
